package com.gitee.llj.shiro.config.shiro;

import com.gitee.llj.entity.sys.SysModule;
import com.gitee.llj.entity.sys.SysUser;
import com.gitee.llj.shiro.service.ISysModuleService;
import com.gitee.llj.shiro.service.ISysUserService;
import com.gitee.llj.tools.base.ResultModel;
import com.gitee.llj.tools.utils.FastJsonUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>用户认证受权</p>
 *
 * @Author llj
 * @Date 2018/3/20
 */
public class UserRealm extends AuthorizingRealm{

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysModuleService sysModuleService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //用户名
        SysUser sysUser = (SysUser) principals.fromRealm(getName()).iterator().next();
        //根据用户名来添加相应的权限和角色
        if(sysUser!=null){
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            //权限
            addPermission(sysUser.getId(),info);
            //角色
//            addRole(username, info);
            return info;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username= (String) token.getPrincipal();
        ResultModel<SysUser> resultModel=sysUserService.getUserInfoByUserName(username);
        SysUser sysUser= resultModel.getData();
        if (sysUser==null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                sysUser, //用户名
                sysUser.getPassword(), //密码
              //  ByteSource.Util.bytes(sysUser.getSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;
    }

    /**
     * 添加权限
     * @param userId
     * @param info
     * @return
     */
    private SimpleAuthorizationInfo addPermission(String userId,SimpleAuthorizationInfo info) {
       ResultModel rm= sysModuleService.getPermissionByUser(userId);
       List<SysModule> permissions=FastJsonUtils.toList(FastJsonUtils.toJSONString(rm.getData()),SysModule.class);
       permissions.forEach(item->{
           if (StringUtils.isNotBlank(item.getPermissionCode())){
               info.addStringPermission(item.getPermissionCode());//添加权限
           }
       });
        return info;
    }

    /**
     * 添加角色
     * @param username
     * @param info
     */
//    private void addRole(String username, SimpleAuthorizationInfo info) {
//        List<Role> roles = roleDao.findByUser(username);
//        if(roles!=null&&roles.size()>0){
//            for (Role role : roles) {
//                info.addRole(role.getRoleName());
//            }
//        }
//    }
}
